package com.wizz.trade.information.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wizz.trade.information.dto.page.GradePageDto;
import com.wizz.trade.information.entity.Grade;
import com.wizz.trade.information.mapper.GradeMapper;
import com.wizz.trade.information.service.GradeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wizz.trade.common.PageVo;
import com.wizz.trade.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 品级表 服务实现类
 * </p>
 *
 * @author fyftless
 * @since 2023-04-02 04:26:06
 */
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {

    @Resource
    private GradeMapper gradeMapper;

    @Override
    public void addGrade(Grade grade) {
        gradeMapper.insert(grade);
    }

    @Override
    public void updateGrade(Grade grade) {
        gradeMapper.updateById(grade);
    }

    @Override
    public void deleteGrade(Integer id) {
        gradeMapper.deleteById(id);
    }

    @Override
    public PageVo<Grade> getPage(GradePageDto gradePageDto) {
        LambdaQueryWrapper<Grade> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(gradePageDto.getName()),Grade::getName, gradePageDto.getName());
        queryWrapper.eq(!Objects.isNull(gradePageDto.getVariety()), Grade::getVariety,gradePageDto.getVariety());
        IPage<Grade> page = gradeMapper.selectPage(PageUtil.initPage(gradePageDto), queryWrapper);
        return new PageVo<>(page);
    }

    @Override
    public List<Grade> getAll() {
        return gradeMapper.selectList(null);
    }

    @Override
    public List<Map<Integer, String>> getNames(Integer varietyId) {
        return gradeMapper.getNames(varietyId);
    }

    @Override
    public List<Map<Integer, String>> getOptions(Integer varietyId) {
        return gradeMapper.getOptions(varietyId);
    }


}
